package com.cskaoyan.service.promoteservice;


import com.cskaoyan.bean.promotebean.Topic;
import com.cskaoyan.bean.promotebean.TopicExample;
import com.cskaoyan.mapper.promotemapper.TopicMapper;
import com.cskaoyan.vo.promotevo.TopicCreateVO;
import com.cskaoyan.vo.promotevo.TopicListVO;
import com.cskaoyan.vo.promotevo.TopicUpdateVO;
import com.cskaoyan.vo.wx.WxTopicDetailVo;
import com.cskaoyan.vo.wx.WxTopicListVo;
import com.cskaoyan.vo.wx.WxTopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: feisl
 * @Date: 2021/5/10 1:16
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;

    @Override
    public TopicListVO queryTopicAll(Integer page, Integer limit, String title, String subtitle, String sort, String order) {
        PageHelper.startPage(page, limit);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (title != null) criteria.andTitleLike("%" + title + "%");
        if (subtitle != null) criteria.andSubtitleLike("%" + subtitle + "%");
        criteria.andDeletedEqualTo(false);

        topicExample.setOrderByClause(sort + " " + order);
        List<Topic> topicList = topicMapper.selectByExample(topicExample);

        PageInfo<Topic> topicPageInfo = new PageInfo<>(topicList);
        long total = topicPageInfo.getSize();

        TopicListVO topicListVO = new TopicListVO();
        topicListVO.setItems(topicList);
        topicListVO.setTotal(total);
        return topicListVO;
    }

    @Override
    public TopicCreateVO insert(Topic topic) {
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topicMapper.insertSelective(topic);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(topic.getAddTime());
        String format1 = simpleDateFormat.format(topic.getUpdateTime());
        TopicCreateVO topicCreateVO = new TopicCreateVO(
                topic.getId(), topic.getTitle(), topic.getSubtitle(), topic.getPrice(),
                topic.getReadCount(), topic.getPicUrl(), format, format1,
                topic.getContent(), topic.getGoods()
        );
        return topicCreateVO;
    }

    @Override
    public TopicUpdateVO update(Topic topic) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        topic.setUpdateTime(new Date());
        String updateTime = simpleDateFormat.format(topic.getUpdateTime());
        String addTime = simpleDateFormat.format(topic.getAddTime());
        topicMapper.updateByPrimaryKeySelective(topic);
        TopicUpdateVO topicUpdateVO = new TopicUpdateVO(
                topic.getId(), topic.getTitle(), topic.getSubtitle(), topic.getPrice(), topic.getReadCount(),
                topic.getPicUrl(), topic.getSortOrder(), addTime, updateTime, topic.getDeleted(),
                topic.getContent(), topic.getGoods()
        );
        return topicUpdateVO;
    }

    @Override
    public void delete(Integer id) {
        topicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public WxTopicListVo list(Integer page, Integer size) {

        List<WxTopicVo> list = topicMapper.selectWxTopicListVo((page - 1) * size, size);
        List<Topic> topics = topicMapper.selectByExample(new TopicExample());
        int count = topics.size();
        WxTopicListVo wxTopicListVo = new WxTopicListVo();
        wxTopicListVo.setCount(count);
        wxTopicListVo.setData(list);
        return wxTopicListVo;
    }

    @Override
    public WxTopicDetailVo detail(Integer id) {

        Topic topic = topicMapper.selectByPrimaryKey(id);
        WxTopicDetailVo wxTopicDetailVo = new WxTopicDetailVo();
        wxTopicDetailVo.setTopic(topic);
        return wxTopicDetailVo;
    }

    @Override
    public List<Topic> related() {

        List<Topic> list = topicMapper.selectLimit(4);

        return list;
    }

}
